<?php
include("../../include/connexion.php");

function run_query_vac($id){
$date = date('Y-m-d');
$query = "SELECT COUNT(*) as nb FROM conges where idemp='$id' AND (validechef=2 OR valideRH = 2) AND datedeb > '$date'";
$rep = mysql_query($query);
$donnees = mysql_fetch_array($rep);
return $donnees['nb'];
}

function run_query_nbvalid($id,$stat){
$date = date('Y-m-d');
$query = "SELECT COUNT(*) as nb FROM conges";
if ($stat==2){
$query_service = " SELECT idService from employe where IDEMP= '$id' ";
$resultat= mysql_query($query_service);
$res = mysql_fetch_array($resultat);
$idServ = $res['idService'];
$query.=" as C left join employe as E on E.idemp=C.idemp where validechef=2 AND datedeb > '$date'";
$rep = mysql_query($query);
$donnees = mysql_fetch_array($rep);}
if ($stat==3){
$query.=" where validechef!=2 AND valideRH = 2 AND datedeb > '$date'";
$rep = mysql_query($query);
$donnees = mysql_fetch_array($rep);
}
return $donnees['nb'];
}

function run_query_conges_attente($idstat){

if($idstat==2){
$valide="validechef";}
else if($idstat==3){
$valide="validerh";}

$query= "SELECT
		E.idemp,
		idconges,
		nomemp,
		prenomemp,
		type,
		datedeb,
		datefin
		from employe as E left join conges as C on E.idemp=C.idemp
		where $valide= 2";
		
if ($idstat==3){
$query.=" AND validechef != 2";
}

$query.=" order by datedeb";

$rep = mysql_query($query);

$i=0;
$tab;
while ($donnees = mysql_fetch_array($rep)){
	$tab[$i]['idemp']=$donnees['idemp'];
	$tab[$i]['idconges']=$donnees['idconges'];
	$tab[$i]['nom']=$donnees['nomemp'];
	$tab[$i]['prenom']=$donnees['prenomemp'];
	$tab[$i]['type']=$donnees['type'];
	$tab[$i]['datedeb']=$donnees['datedeb'];
	$tab[$i]['datefin']=$donnees['datefin'];
	$i++;
}
return $tab;
}

function run_query_valid_conges($tab,$idstat){

if($idstat==2){
$valide="validechef";}
else if($idstat==3){
$valide="validerh";}

foreach ($tab as $ind => $val){
$query="UPDATE conges SET $valide=$val WHERE idconges=$ind";
mysql_query($query);
}
}

function run_query_afficher_conges($id,$idstat,$req){

$query = " SELECT 
		E.idemp,
		nomemp,
		prenomemp, 
		nomservice,
		type, 
		datedeb, 
		datefin, 
		nbjours, 
		validechef, 
		validerh 
		from employe as E left join conges as C on E.idemp=C.idemp left join service as S on S.idService = E.idService";

if($idstat==2){
	$query_service = " SELECT idService from employe where IDEMP= '$id' "; 
	$resultat= mysql_query($query_service);
	$res = mysql_fetch_array($resultat);
	$idServ = $res['idService'];
	$query.= " where datedeb!='NULL' AND datefin!='NULL' AND S.idService = $idServ ";
}

$query.= $req;

if($idstat==3){
$query.=" datedeb!='NULL' AND datefin!='NULL'";
}

$rep = mysql_query($query);
 
$i=0;
$tab;
while ($donnees = mysql_fetch_array($rep)){
	$tab[$i]['idemp']=$donnees['idemp'];
	$tab[$i]['nomemp']=$donnees['nomemp'];
	$tab[$i]['prenomemp']=$donnees['prenomemp'];
	$tab[$i]['nomservice']=$donnees['nomservice'];
	$tab[$i]['type']=$donnees['type'];
	$tab[$i]['datedeb']=$donnees['datedeb'];
	$tab[$i]['datefin']=$donnees['datefin'];
	$tab[$i]['nbjours']=$donnees['nbjours'];
	$tab[$i]['validechef']=$donnees['validechef'];
	$tab[$i]['validerh']=$donnees['validerh'];
	$i++;
}
return $tab;
}

?>